package com.bd.module.login.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;

import com.bd.framework.pojo.OperatorInfo;
import com.bd.framework.utils.database.CheckConnection;
import com.bd.framework.utils.database.SQliteConnection;
import com.bd.module.BaseFrame;
import com.bd.module.login.server.impl.OperatorImpl;

import javax.swing.SwingConstants;
/**
 * @author hnxdyl@hotmail.com
 * pos收银登陆对话框
 * */
public class LoginJDialog extends JDialog{
	private static final long serialVersionUID = -8512995026380207231L;
	/**用户编码*/
	private final JTextField userIdLid;
	/**用户名称*/
	private final JTextField userNamLid;
	/**用户密码*/
	private final JPasswordField userPwdLid;
	/**按钮字体*/
	private final Font BTN_FONT = new Font("楷体", Font.PLAIN, 20);
	/**JLabel标签的字体样式*/
	private final Font LID_FONT = new Font("楷体", Font.PLAIN, 20);
	/**设置button,jlabel,jtextField的宽度和高度*/
	private int btnWidth = 106;
	private int btnHeight = 28;
	private int jLabelWidth = 103;
	private int jLabelHeight = 21;
	private int jTextFildWidth = 100;
	private int jTextFildHeight = 21;
	/**操作员查询接口*/
	private OperatorImpl operatorImpl = new OperatorImpl();
	private OperatorInfo getOpInfo;
	private static LoginJDialog lg;
	static  public   LoginJDialog getLoginJDialog(){
		if(lg==null){
			lg=new LoginJDialog();
		}
		return lg;
	}
	
	public LoginJDialog() {
		this.setUndecorated(true);
		getRootPane().setWindowDecorationStyle(JRootPane.COLOR_CHOOSER_DIALOG);
		setPreferredSize(new Dimension(400, 200));
		//setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
		getContentPane().setLayout(null);
		
		final JLabel messLabel = new JLabel("");
		messLabel.setHorizontalAlignment(SwingConstants.CENTER);
		messLabel.setBounds(60, 10, 197, 21);
		getContentPane().add(messLabel);
		messLabel.setFont(new Font("楷体", Font.PLAIN, 20));
		messLabel.setForeground(Color.red);
		
		final JLabel lblid = new JLabel("用户编码:");
		lblid.setHorizontalAlignment(SwingConstants.LEFT);
		lblid.setBounds(47, 41, this.jLabelWidth, this.jLabelHeight);
		getContentPane().add(lblid);
		lblid.setFont(LID_FONT);
		
		userIdLid = new JTextField();
		userIdLid.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent keyEvent) {
				//界面初始化之后光标位于用户编码的输入框,处理不输入直接使用快捷键退出系统和加载系统的ini文件
				if(keyEvent.getKeyCode() == KeyEvent.VK_Q && keyEvent.isAltDown()){
					System.exit(0);
				}
				if(keyEvent.getKeyCode() == KeyEvent.VK_S && keyEvent.isAltDown()){
					//设置系统的ini文件
					new IniReader().showFrame();
				}
				//每次输入完成需要点击Enter键,驱动光标转移
				if(keyEvent.getKeyCode() == KeyEvent.VK_ENTER){
					if(!"".equals(messLabel.getText())){
						messLabel.setText("");
					}
					OperatorInfo  operInfo = new OperatorInfo();
					operInfo.setOpCode(userIdLid.getText().trim());
					/**
					 * 输入用户编码,输入enter则校验用户编码是否正常,并返回用户名称。通知驱动光标指向用户密码输入框
					 * */
					getOpInfo = operatorImpl.checkOperInfo(operInfo);
					if(getOpInfo.getOpName() != null){
						userNamLid.setText(getOpInfo.getOpName());
						userPwdLid.requestFocus();
					}else{
						messLabel.setText("用户编码错误!");
					}
				}
				/**
				 * 界面初始化之后光标位于用户编码的输入框,处理不输入直接登录系统
				 * 判断当前的光标是否转移,如果光标在userIdLid上直接提示信息错误
				 * */
				if(keyEvent.getKeyCode() == KeyEvent.VK_E && keyEvent.isAltDown()){
					if("".equals(userNamLid.getText())){
						messLabel.setText("用户编码不能为空!");
					}
				}
			}
		});
		userIdLid.setHorizontalAlignment(SwingConstants.LEFT);
		userIdLid.setBounds(160, 41, jTextFildWidth, jTextFildHeight);
		getContentPane().add(userIdLid);
		userIdLid.setColumns(10);
		
		final JLabel label = new JLabel("用户名称:");
		label.setHorizontalAlignment(SwingConstants.LEFT);
		label.setBounds(47, 72, this.jLabelWidth, this.jLabelHeight);
		getContentPane().add(label);
		label.setFont(LID_FONT);
		
		userNamLid = new JTextField();
		userNamLid.setHorizontalAlignment(SwingConstants.LEFT);
		userNamLid.setEditable(false);
		userNamLid.setBounds(160, 72, jTextFildWidth, jTextFildHeight);
		getContentPane().add(userNamLid);
		userNamLid.setColumns(10);
		
		final JLabel label_1 = new JLabel("用户密码:");
		label_1.setHorizontalAlignment(SwingConstants.LEFT);
		label_1.setBounds(47, 104, this.jLabelWidth, this.jLabelHeight);
		getContentPane().add(label_1);
		label_1.setFont(LID_FONT);
		
		userPwdLid = new JPasswordField();
		userPwdLid.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent keyEvent) {
				//光标位于用户密码的输入框,处理不输入直接使用快捷键退出系统和加载系统的ini文件
				if(keyEvent.getKeyCode() == KeyEvent.VK_Q && keyEvent.isAltDown()){
					System.exit(0);
				}
				if(keyEvent.getKeyCode() == KeyEvent.VK_S && keyEvent.isAltDown()){
					//设置系统的ini文件
					new IniReader().showFrame();
				}
				//每次输入完成需要点击Enter键,驱动光标转移baseFrame
				if(keyEvent.getKeyCode() == KeyEvent.VK_ENTER){
					if(!"".equals(messLabel.getText())){
						messLabel.setText("");
					}
					String opPass = new String(userPwdLid.getPassword());
					if(opPass.equals(getOpInfo.getOpPass().toString().trim())){
						BaseFrame.getBaseFrame().setTitle("百大收银系统南昌分店    操作员编码:"+getOpInfo.getOpCode()+",操作员:"
								+ getOpInfo.getOpName());//设置当前客户端的title
						//获得父窗体Card模式显示指定panel
						BaseFrame.getBaseFrame().getBaseCard().show(BaseFrame.getBaseFrame().getBasePanel(),"functionMainPanl");
						dispose();
						BaseFrame.getBaseFrame().setFocusable(true);
					}else{
						messLabel.setText("用户密码错误!");
					}
				}
				/**
				 * 光标位于用户密码的输入框,处理不输入直接登录系统
				 * 判断当前的光标是否转移,如果光标在userIdLid上直接提示信息错误
				 * */
				if(keyEvent.getKeyCode() == KeyEvent.VK_E && keyEvent.isAltDown()){
					if("".equals(userPwdLid.getPassword().toString())){
						messLabel.setText("用户密码不能为空!");
					}
				}
			}
		});
		userPwdLid.setHorizontalAlignment(SwingConstants.LEFT);
		userPwdLid.setBounds(160, 104, jTextFildWidth, jTextFildHeight);
		getContentPane().add(userPwdLid);
		userPwdLid.setColumns(10);
		
//		JButton loginBtn = new JButton("登陆(E)");
//		loginBtn.setBounds(115, 123, this.btnWidth, this.btnHeight);
//		getContentPane().add(loginBtn);
//		loginBtn.setFont(BTN_FONT);
		
		JButton setBtn = new JButton("设置(S)");
		setBtn.setBounds(33, 135,this.btnWidth, this.btnHeight);
		getContentPane().add(setBtn);
		setBtn.setFont(BTN_FONT);
		
		JButton btnn = new JButton("退出(Q)");
		btnn.setBounds(170, 135, this.btnWidth, this.btnHeight);
		getContentPane().add(btnn);
		btnn.setFont(BTN_FONT);
	}
}
